package com.shiyi.controller;

import com.shiyi.annotations.DynamicDataSource;
import com.shiyi.entity.User;
import com.shiyi.enums.DataSourceType;
import com.shiyi.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author wangzhanshuai
 * @date 2021/3/5 4:11 上午
 * @since 1.0
 */
@RestController
@RequestMapping("/dynamic")
public class DynamicDataSourceController {

    @Resource
    private UserMapper userMapper;

    @GetMapping("/master/{id}")
    @DynamicDataSource(type = DataSourceType.MASTER_DB)
    public User testMasterDataSource(@PathVariable("id") Long id) {
        return userMapper.getUserById(id);
    }

    @GetMapping("/slave/{id}")
    @DynamicDataSource(type = DataSourceType.SLAVE_DB)
    public User testSlaveDataSource(@PathVariable("id") Long id) {
        return userMapper.getUserById(id);
    }
}
